//
//  OSPlaceholderTextView.m
//  百思不得姐
//
//  Created by PushMeBaby on 16/3/8.
//  Copyright © 2016年 CoderC. All rights reserved.
//

#import "OSPlaceholderTextView.h"

@implementation OSPlaceholderTextView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.alwaysBounceVertical = YES;
        self.font = [UIFont systemFontOfSize:15];
        
        self.pladeholderColor = [UIColor grayColor];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}
-(void)textDidChange{
    [self setNeedsDisplay];
}

-(void)drawRect:(CGRect)rect{
    if (self.hasText) {
        return;
    }
    
    rect.origin.x = 4;
    rect.origin.y = 7;
    rect.size.width -= 2 * rect.origin.x;
    
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = self.font;
    attrs[NSForegroundColorAttributeName] = self.pladeholderColor;
    
    [self.placeholder drawInRect:rect withAttributes:attrs];
}
-(void)setPlaceholder:(NSString *)placeholder{
    _placeholder = [placeholder copy];
    [self setNeedsDisplay];
}
-(void)setPladeholderColor:(UIColor *)pladeholderColor{
    _pladeholderColor = pladeholderColor;
    [self setNeedsDisplay];
}
-(void)setFont:(UIFont *)font{
    [super setFont:font];
    [self setNeedsDisplay];
}
-(void)setText:(NSString *)text{
    [super setText:text];
    [self setNeedsDisplay];
}
-(void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    [self setNeedsDisplay];
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
